package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.astrob.activitys.AppContext;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;
import com.gpx2shp.JniUtils;
import com.nhc.database.dbmodel.Route;
import com.nhc.database.dbmodel.WayPoint;
import com.nhc.database.dbmodel.WayTrail;
import com.nhc.database.manager.DBDataCenter;
import com.nhc.database.manager.IECenter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;

public class ImportExportActivity extends BaseActivity implements View.OnClickListener {
    private final String TAG = "ImportExportActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, ImportExportActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.importexport_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    @Override
    public void initView() {//必须调用
        TextView tv_icon0 = (TextView) findViewById(R.id.tv_icon0);
        TextView tv_icon1 = (TextView) findViewById(R.id.tv_icon1);
        TextView tv_icon2 = (TextView) findViewById(R.id.tv_icon2);
        tv_icon0.setTypeface(AppContext.getInstances().getIconfont());
        tv_icon1.setTypeface(AppContext.getInstances().getIconfont());
        tv_icon2.setTypeface(AppContext.getInstances().getIconfont());

    }

    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initData() {//必须调用

    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须调用
        findViewById(R.id.ll_import).setOnClickListener(this);
        findViewById(R.id.ll_export).setOnClickListener(this);
        findViewById(R.id.ll_export2).setOnClickListener(this);

    }
    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    //类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:
                break;
            case 21://left
            case 22://right
            case 19://up
            case 20://down
            case 23://ok
                setFoucseCtrl();
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    boolean _isFirst = true;

    private void setFoucseCtrl() {
        if (!_isFirst) {
            return;
        }

        _isFirst = false;

        for (int i = 0; i < CTRL_IDS.length; i++) {
            findViewById(CTRL_IDS[i]).setFocusable(true);
        }
    }

    private int[] CTRL_IDS = {
            R.id.ll_import, R.id.ll_export, R.id.ll_export2
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_import:
                onImport();
                break;
            case R.id.ll_export:
                onExport();
                break;
            case R.id.ll_export2:
                onExport2();
                break;
            default:
                break;
        }
    }

    private void onImport() {
        toActivity(DaoruActivity.createIntent(this), REQUEST_TO_DAORU);
    }

    private void onExport() {
        toActivity(DaochuActivity.createIntent(this), REQUEST_TO_DAOCHU);
    }

    private void onExport2() {
        showProgressDialog(R.string.loading);
        runThread(TAG + "daochuData", new Runnable() {
            @Override
            public void run() {
                final boolean isok = IECenter.getInstance().export2GPX_today();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        if (isok) {
                            showShortToast("已导出每日数据！");
                        } else {
                            showShortToast("无数据导出！");
                        }

                    }
                });
            }
        });
    }

    //类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    private final int REQUEST_TO_DAOCHU = 30;
    private final int REQUEST_TO_DAORU = 31;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case REQUEST_TO_DAOCHU:
                finish();
                break;
            case REQUEST_TO_DAORU:
                finish();
                break;
        }
    }

}
